package com.luoye_lj.dogcontroller.util;
public class CountdownTimer {
    public static interface Ticker{
        void tick(int remainSeconds);
    }
    int totalSeconds;
    Ticker ticker;
    Thread thread;
    boolean stopped;

    public CountdownTimer(int totalSeconds,Ticker ticker1) {
        this.totalSeconds = totalSeconds;
        this.ticker=ticker1;
        thread=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=totalSeconds;i>=0;i--){
                    if (stopped) {
                        return;
                    }
                    ticker.tick(i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
    }

    public void start(){
        this.thread.start();
    }

    public void stop(){
        this.stopped=true;
    }

    public void tick(int remainSeconds){
        this.ticker.tick(remainSeconds);
    }
}
